package test;

import org.springframework.util.FileSystemUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class LabelCopy {

    public static void main(String[] args) throws IOException {
        //读取原有的标签
        String srcPath = "/Users/hym/Desktop/score/labels/val/";
        File srcDir = new File(srcPath);
        File[] labelFiles = srcDir.listFiles();
        if (Objects.isNull(labelFiles)) {
            return;
        }
        Map<String, File> name2label = new HashMap<>();
        for (File label : labelFiles) {
            String fileName = label.getName();
            int dotIndex = fileName.indexOf('.');
            String name = fileName.substring(0, dotIndex);
            name2label.put(name, label);
        }

        //读取加强后的图片
        String picPath = "/Users/hym/Desktop/score_hiscombina/images/val/";
        String destPath = "/Users/hym/Desktop/score_hiscombina/labels/val/";
        File picDir = new File(picPath);
        File[] picFiles = picDir.listFiles();
        if (Objects.isNull(picFiles)) {
            return;
        }
        for (File pic : picFiles) {
            String fileName = pic.getName();
            if (fileName.startsWith(".")) {
                continue;
            }
            int splitIndex = fileName.indexOf('_');
            int dotIndex = fileName.indexOf('.');
            String name = fileName.substring(splitIndex+1, dotIndex);
            File srcLabel = name2label.get(name);
            if (Objects.isNull(srcLabel)) {
                continue;
            }
            //构造拷贝的文件
            File destFile = new File(destPath+fileName.substring(0, dotIndex+1) + "txt");
            FileSystemUtils.copyRecursively(srcLabel, destFile);
        }
    }

}
